package com.example.community.rest;

import com.example.community.cache.TagCache;
import com.example.community.dto.ResultDTO;
import com.example.community.dto.UserDto;
import com.example.community.exception.CustomizeErrorCode;
import com.example.community.dto.QuestionDTO;
import com.example.community.model.Question;
import com.example.community.service.QuestionService;
import com.example.community.util.CookieChecking;
import com.example.community.util.GetIpAddress;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(path = "/publish",
        produces = "application/json")
public class PublishQuestionResource {

    @Autowired
    private QuestionService questionService;

//    @GetMapping("/{id}")
//    public String edit(@PathVariable(name = "id") Long id,
//                       Model model) {
//        QuestionDTO question = questionService.getById(id);
//        model.addAttribute("title", question.getTitle());
//        model.addAttribute("description", question.getDescription());
//        model.addAttribute("tag", question.getTag());
//        model.addAttribute("id", question.getId());
//        model.addAttribute("tags", TagCache.get());
//        return "publish";
//    }


//    @GetMapping
//    public String publish(Model model) {
//        model.addAttribute("tags", TagCache.get());
//        return "publish";
//    }

    @PostMapping(consumes = "application/json")
    public Object doPublish(
            @RequestBody QuestionDTO questionDTO,
            HttpServletRequest request,
            Model model) {
        if (questionDTO.getId()!= null){
            String checkToken = CookieChecking.CookieToken(request.getCookies(), "token");
            String createType = questionService.getCreateType(questionDTO.getId());
            if (!createType.equals(checkToken.substring(0,2))){
                model.addAttribute("message","用户权限错误!");
                return "error";
            }
        }

        model.addAttribute("title", questionDTO.getTitle());
        model.addAttribute("description", questionDTO.getDescription());
        model.addAttribute("tag", questionDTO.getTag());
        model.addAttribute("tags", TagCache.get());
        if (StringUtils.isBlank(questionDTO.getTitle())) {
            model.addAttribute("error", "标题不能为空");
            return ResultDTO.errorOf(CustomizeErrorCode.QUESTION_TITLE_IS_EMPTY);
        }

        if (StringUtils.length(questionDTO.getTitle()) > 50) {
            model.addAttribute("error", "标题最多 50 个字符");
            return ResultDTO.errorOf(CustomizeErrorCode.QUESTION_TITLE_OVERFLOW);
        }
        if (StringUtils.isBlank(questionDTO.getDescription())) {
            model.addAttribute("error", "问题补充不能为空");
            return ResultDTO.errorOf(CustomizeErrorCode.QUESTION_DESCRIPTION_IS_EMPTY);
        }
        if (StringUtils.isBlank(questionDTO.getTag())) {
            model.addAttribute("error", "标签不能为空");
            return ResultDTO.errorOf(CustomizeErrorCode.QUESTION_TAG_IS_EMPTY);
        }

        String invalid = TagCache.filterInvalid(questionDTO.getTag());
        if (StringUtils.isNotBlank(invalid)) {
            model.addAttribute("error", "输入非法标签:" + invalid);
            return ResultDTO.errorOf(CustomizeErrorCode.QUESTION_ERROR_TAG);
        }
        UserDto user = (UserDto)request.getSession().getAttribute("user");
        if (user == null) {
            model.addAttribute("error", "用户未登录");
            return ResultDTO.errorOf(CustomizeErrorCode.NO_LOGIN);
        }
        Question question = new Question();
        Cookie[] cookies = request.getCookies();
        String token = CookieChecking.CookieToken(cookies,"token");
        question.setCreatorType(token.substring(0,2));
        question.setTitle(questionDTO.getTitle());
        question.setDescription(questionDTO.getDescription());
        question.setTag(questionDTO.getTag());
        question.setCreator(user.getId());
        question.setId(questionDTO.getId());
        question.setSticky(0);
        question.setIp(user.getIp());
        questionService.createOrUpdate(question, user.getIp());
        return ResultDTO.okOf();
    }
}
